﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services.Protocols;
using rsdeploygui.ReportService2010;

namespace rsdeploygui
{
    public class CatalogItem
    {
        private ReportingService2010 rs;

        public CatalogItem(string url)
        {
            try
            {
                rs = new ReportingService2010();
                rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
                rs.Url = url;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public ReportService2010.CatalogItem[] ListChildren(string path)
        {
            try
            {
                return this.rs.ListChildren(path, false);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }

        public ReportService2010.CatalogItem[] ListParents(string path)
        {
            return this.rs.ListParents(path);
        }

        public string GetReportServerName()
        {
            Uri Server = new Uri(this.rs.Url.ToString());
            return Server.Host;
        }
        public string GetItemType(string path)
        {
            return this.rs.GetItemType(path);
        }

        /// <summary>
        /// Create catalog item
        /// </summary>
        /// <param name="Type"></param>
        /// <param name="Name"></param>
        /// <param name="Parent"></param>
        /// <param name="Overwrite"></param>
        /// <param name="ciDef"></param>
        /// <returns></returns>
        public Boolean Create(string Type, string Name, string Parent, bool Overwrite, byte[] ciDef)
        {
            Boolean success = false;
            ReportService2010.Warning[] warnings = null;
            try
            {
                this.rs.CreateCatalogItem(Type, Name, Parent, Overwrite, ciDef, null, out warnings);
                success = true;
            }
            catch (SoapException e)
            {
                Console.WriteLine(e.Detail.InnerText);
            }
            return success;
        }

        public void ViewProperties(string ItemPath)
        {
            try
            {
                Property[] properties = this.rs.GetProperties(ItemPath, null);
                foreach (Property prop in properties)
                {
                    Console.WriteLine("{0} : {1}", prop.Name, prop.Value);
                }
            }
            catch (SoapException e)
            {
                Console.WriteLine(e.Detail.InnerText);
            }

        }
        /// <summary>
        /// Get the catalogitem definition
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public byte[] GetDef(string path)
        {
            byte[] DefOut = null;

            try
            {
                DefOut = this.rs.GetItemDefinition(path);
            }
            catch (SoapException e)
            {
                Console.WriteLine(e.Detail.InnerText);
            }
            return DefOut;
        }
    }
}
